package com.cskaoyan.service;

import com.cskaoyan.bean.BO.IssueCreateBo;
import com.cskaoyan.bean.PO.MarketIssue;
import com.cskaoyan.bean.PO.MarketIssueExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminIssueServiceImpl
 * @Description TODO
 * @Author ke
 * @Date 2022/8/13 15:45
 * @Version 1.0
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;


    @Override
    public CommonData<MarketIssue> list(BasePageInfo info, String question) {
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        if (question != null) {
            criteria.andQuestionLike("%"+question+"%");
        }

        criteria.andDeletedEqualTo(false);

        // 在查询前进行分页操作
        PageHelper.startPage(info.getPage(),info.getLimit());

        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        // example.setOrderByClause("id desc");

        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketIssues);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketIssue create(IssueCreateBo bo) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(bo.getQuestion());
        marketIssue.setAnswer(bo.getAnswer());
        marketIssue.setAddTime(new Date());
        marketIssue.setUpdateTime(new Date());
        marketIssueMapper.insertSelective(marketIssue);
        return marketIssue;
    }

    @Override
    public MarketIssue update(MarketIssue bo) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(bo.getId());
        marketIssue.setQuestion(bo.getQuestion());
        marketIssue.setAnswer(bo.getAnswer());
        marketIssue.setUpdateTime(new Date());
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        return marketIssue;
    }

    @Override
    public MarketIssue delete(MarketIssue bo) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(bo.getId());
        marketIssue.setDeleted(true);
        marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        return marketIssue;
    }
}
